package com.indigon.parkStory.model.objects
{
	import com.indigon.parkStory.model.ModelMain;
	
	import flash.geom.Point;
	import flash.utils.Dictionary;

	public class WaveModel extends ObjectModel
	{
		private static const DEFAULT_RADIUS        : Number = 10;
		private static const FORCE_DURA_MULTIPLIER : Number = 45;
		private static const FORCE_MULTIPLIER      : Number = 1.3;
		private static const GROWTH_MULTIPLIER     : Number = 4;
		
		private var _duration : uint;
		private var _counter  : uint;
		private var _force    : Number;
		private var _growth   : Number;
		public var radius     : Number;
		public var affected   : Dictionary;
		
		public function WaveModel(x:Number, y:Number, force:Number, generatedBy:LillyModel, model:ModelMain)
		{
			this._force = force;
			this._growth = force * GROWTH_MULTIPLIER;
			this._duration = uint(force * FORCE_DURA_MULTIPLIER);
			this.affected = new Dictionary();
			
			if (generatedBy != null)
			{
				this.radius = generatedBy.data.radius;
				affected[generatedBy] = true;
			}
			else
			{
				this.radius = DEFAULT_RADIUS;
			}
			super(model);
			set(x, y);
		}
		
		public override function uninit():void
		{
			affected = null;
			super.uninit();
		}
		
		public override function update():void
		{
			_counter++;
			if (_counter == _duration)
			{
				isDead = true;
			}
			else
			{
				radius += _growth;
				for each (var lilly : LillyModel in _model.lillies)
				{
					if ((affected[lilly] == null) && lilly.isForceable)
					{
						var d 		 : Point  = lilly.pos.subtract(this.pos);
						var dist_sqr : Number = (d.x * d.x) + (d.y * d.y);
						var rad      : Number = this.radius + lilly.data.radius;
						var rad_sqr  : Number = rad * rad;
						if (dist_sqr <= rad_sqr)
						{
							affected[lilly] = true;
							d.normalize(_force * value);
							lilly.addForce(d);
						}
					}
				}
			}
		}
		
		public function get value():Number
		{
			return 1 - (_counter / _duration);
		}
	}
}